package com.example.rs485demo.util;
//import gnu.io.SerialPort;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import purejavacomm.SerialPort;

import java.util.ArrayList;


/**
 * Demo class
 *
 * @author XDRS
 * @date 2023/3/30 16:01
 */
@Component
public class PortInit implements ApplicationRunner{
 
    public static SerialPort serialPort = null;
 
 
    @Override
    public void run(ApplicationArguments args) {
        String portName = "COM3";
        //TestA();
        //查看所有串口
        SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil();
        ArrayList<String> port = serialPortUtil.findPort();
        System.out.println("发现全部串口：" + port);
 
        System.out.println("portName:" + portName);
        //打开该对应portName名字的串口
        PortInit.serialPort = serialPortUtil.openPort(portName, 9600, SerialPort.DATABITS_8, SerialPort.PARITY_NONE, SerialPort.PARITY_ODD);
 
        // 给对应的serialPort添加监听器
        serialPortUtil.addListener(PortInit.serialPort, new MyLister());
 
    }
 
}